package com.lsh.oms.worker.service.task;

import com.dangdang.ddframe.job.api.JobExecutionMultipleShardingContext;
import com.dangdang.ddframe.job.plugin.job.type.simple.AbstractSimpleElasticJob;
import com.lsh.oms.core.email.SendEmailService;
import com.lsh.oms.core.enums.TaskType;
import com.lsh.oms.core.model.order.OrderSignHead;
import com.lsh.oms.core.model.po.OmsTask;
import com.lsh.oms.core.service.finBill.FinBillService;
import com.lsh.oms.core.service.po.OmsTaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * Project Name: lsh-oms
 *
 * @author peter
 * @date 18/2/27
 * 北京链商电子商务有限公司
 * Package:
 * desc:
 */
@Slf4j
@Component
@Deprecated
public class BillCreateDayJop extends AbstractSimpleElasticJob {

    @Autowired
    private FinBillService finBillService;

    @Autowired
    private SendEmailService sendEmailService;

    @Autowired
    private OmsTaskService omsTaskService;

    @Override
    public void process(JobExecutionMultipleShardingContext jobExecutionMultipleShardingContext) {

        try {

            Integer startTime = this.getStartTime();
            Integer endTime = this.getEndTime(startTime, 24 * 60 * 60);
            log.info("【BillDay下单】统计范围 {} - {} ", startTime, endTime);
            log.info("【day处理签收单task】 【开始】");
            this.addSignTask(startTime, endTime);
            log.info("【day处理签收单task】 【结束】");
            log.info("【day处理反仓单task】 【开始】");
            this.addRoTask(startTime, endTime);
            log.info("【day处理反仓单task】 【结束】");

        } catch (Exception ex) {

//            String message = ex.getMessage();
//            if (message.length() > 100) {
//                message = message.substring(0, 100);
//            }
//
//            this.sendEmailService.asyncSendEmail(message, "创建Bill异常");

            log.error("[omsTask 添加] 异常", ex);
        }

    }


    private boolean addSignTask(Integer startTime, Integer endTime) {
        List<OrderSignHead> orderSignHeadList = finBillService.selectSignHeads(startTime, endTime);
        List<OmsTask> omsTaskList = omsTaskService.getTasks(startTime, endTime, TaskType.Bill_SIGN.getCode());
        Map<String, String> omstaskMap = new HashMap<>();
        for (OmsTask omsTask : omsTaskList) {
            omstaskMap.put(omsTask.getRefCode(), omsTask.getRefCode());
        }

        Iterator<OrderSignHead> orderSignHeadIterator = orderSignHeadList.iterator();

        while (orderSignHeadIterator.hasNext()) {
            OrderSignHead orderSignHead = orderSignHeadIterator.next();

            if (StringUtils.isNotBlank(omstaskMap.get(String.valueOf(orderSignHead.getReceiptOrderId())))) {
                orderSignHeadIterator.remove();
            }
        }

        if (orderSignHeadList.isEmpty()) {
            return true;
        }

        finBillService.addSignTask(orderSignHeadList);

        return true;
    }


    private boolean addRoTask(Integer startTime, Integer endTime) {
        List<Long> returnOrderIds = finBillService.selectRoIds(startTime, endTime);
        List<OmsTask> omsTaskList = omsTaskService.getTasks(startTime, endTime, TaskType.Bill_RO.getCode());
        Map<String, String> omstaskMap = new HashMap<>();
        for (OmsTask omsTask : omsTaskList) {
            omstaskMap.put(omsTask.getRefCode(), omsTask.getRefCode());
        }

        Iterator<Long> returnOrderIdsIt = returnOrderIds.iterator();
        while (returnOrderIdsIt.hasNext()) {
            Long returnOrderId = returnOrderIdsIt.next();

            if (StringUtils.isNotBlank(omstaskMap.get(String.valueOf(returnOrderId)))) {
                returnOrderIdsIt.remove();
            }
        }

        finBillService.addRoTask(returnOrderIds);


        return true;
    }


    /**
     * 统计开始时间
     *
     * @return 时间戳
     */
    private Integer getStartTime() {

        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.DATE, -1);

        return (int) TimeUnit.MILLISECONDS.toSeconds(cal.getTime().getTime());
    }

    /**
     * 统计结束时间
     *
     * @param timePoint 时间点
     * @return 时间戳
     */
    private Integer getEndTime(int startTime, int timePoint) {

        return startTime + timePoint;
    }
}
